package com.cloudpivot.receivales;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.controller.DingBasicController;
import com.cloudpivot.receivales.ding.basic.service.DingBasicService;
import com.cloudpivot.receivales.ding.basic.service.impl.HttpAPIServiceForDing;
import com.cloudpivot.receivales.ding.scenegroup.controller.ScenegroupController;
import com.cloudpivot.receivales.ding.scenegroup.domain.*;
import com.cloudpivot.receivales.ding.scenegroup.service.CrmDingService;
import com.cloudpivot.receivales.ding.scenegroup.service.DriveSpacesService;
import com.cloudpivot.receivales.ding.scenegroup.service.ScenegroupService;
import com.cloudpivot.receivales.ding.userinfo.service.DingUserService;
import com.dingtalk.api.response.OapiMediaUploadResponse;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

import static com.cloudpivot.receivales.ding.util.DingUtil.listToString;

@SpringBootTest
class CloudpivotApplicationTestForDingFile {

    @Autowired
    private DingBasicService dingBasicService;
    @Autowired
    private DingUserService dingUserService;

    @Autowired
    private ScenegroupService scenegroupService;

    @Autowired
    private ScenegroupController scenegroupController;

    @Autowired
    private DriveSpacesService driveSpacesService;

    @Autowired
    private CrmDingService crmDingService;

    @Autowired
    private DingBasicController dingBasicController;
    @Autowired
    private HttpAPIServiceForDing httpAPIServiceForDing;


    /**
     * 注册钉钉http回调
     */
    @Test
    public void testRegisterDingCallBack() {
//        dingBasicController.registerCallBack();
//        dingBasicController.getCallBack();
    }


    /**
     * 创建 机器人发送信息 文件类
     * @lAjPDe7s4XGbL5POBQclbc5BNwIG
     */
    @Test
    public void testCreateGroupRobot() {
        JSONObject _temp = new JSONObject();
        JSONObject photo = new JSONObject();
        photo.put("photoURL","@lAjPDe7s4XGbL5POBQclbc5BNwIG");
        _temp.put("msgtype", "file");
        _temp.put("photo", photo);


        GroupRobotDTO entity = new GroupRobotDTO();
        entity.setTargetOpenConversationId("cidJEEj0lmPX51GXORZW8Mt7w==");
        entity.setMsgTemplateId(JSONObject.toJSONString(_temp));
        JSONObject msgTemp = new JSONObject();
//        msgTemp.put("img_media_id", "@lAjPDe7s4XGbL5POBQclbc5BNwIG");
//        msgTemp.put("title", "来自机器人的提醒");
        entity.setMsgParamMap(msgTemp.toJSONString());
        entity.setIsAtAll(true);
        entity.setRobotCode("NU1bMfDvPNVNvB216538891750381031");
        scenegroupService.sendMsgByGroupRobot(entity);
    }

    /**
     * 测试钉钉上传文件
     */
    @Test
    public void testDingFileUpload() {
        //获取本地文件测试
        byte[] buffer = new byte[0];
        try {
            File file = new File("/Users/kent/Downloads/testdoc.docx");
            FileInputStream inputStream = new FileInputStream(file);
            buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();

            DingUpdateFileReqDTO entity = new DingUpdateFileReqDTO();
            entity.setName("testdoc");
            entity.setBytes(buffer);
            entity.setType("file");

            OapiMediaUploadResponse rsp=driveSpacesService.dingMediaUpload(entity);
            System.out.println("返回：" + JSONObject.toJSONString(rsp));


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
